package org.example.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author 季舟
 * @Date 2024-09-05 16:04
 */
@Aspect
@Component
@Slf4j
public class TimeAspect {

    @Around("execution(* org.example.controller.*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法执行前的业务逻辑
        long startTime = System.currentTimeMillis();

        //执行原始方法（即连接点），并且接收原始方法的返回值
        Object object = joinPoint.proceed();

        //方法执行后的业务逻辑
        long endTime = System.currentTimeMillis();
        log.info("{}:耗时{}ms",object.toString(),endTime-startTime);

        //返回原始方法的返回值
        return object;
    }
}
